package com.atguigu.gateway.api.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.cors.CorsConfiguration;
import org.springframework.web.cors.reactive.CorsWebFilter;
import org.springframework.web.cors.reactive.UrlBasedCorsConfigurationSource;

/**
 * @Author zhaoshuo
 * @ClassName CorsConfig
 * @Description 常用组件类注解：  可以创建对象注入到容器中
 * spring+springmvc会自动扫描的注解：
 * > Controller  Service  Repository Component
 * > RestController ControllerAdvice  RestControllerAdvice
 * > Configuration
 * 第三方的扫描bean的注解：
 * >Mapper   MapperScan
 * >FeignClient  EnableFeignClient
 * >ServletComponentScan:扫描 tomcat的组件(filter  servlet  listener)
 * springboot创建bean到容器中的方式：
 * 1、组件注解
 * 2、@Bean标注的方法返回值
 */
@Configuration
public class CorsConfig {

    @Bean
    public CorsWebFilter corsWebFilter() {
        UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
        CorsConfiguration config = new CorsConfiguration();
        config.addAllowedOrigin("http://manager.gmall.com");
        config.addAllowedOrigin("http://www.gmall.com");
        config.addAllowedHeader("*");
        config.addAllowedMethod("*");
        config.setAllowCredentials(true);
        source.registerCorsConfiguration("/**", config);

        return new CorsWebFilter(source);
    }

}